package com.dime.basic.service.impl;

import com.dime.basic.api.domain.*;
import com.dime.basic.mapper.*;
import com.dime.basic.service.IDimeBaseoccupationcriterionService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 职业诊断标准Service业务层处理
 *
 * @author Tiny
 * @date 2023-10-13
 */
@Service
public class DimeBaseoccupationcriterionServiceImpl implements IDimeBaseoccupationcriterionService {
    @Autowired
    private DimeBaseoccupationcriterionMapper dimeBaseoccupationcriterionMapper;

    @Autowired
    private DimeBasecombcriterionMapper dimeBasecombcriterionMapper;

    @Autowired DimeBasecombMapper dimeBasecombMapper;

    @Autowired
    private DimeBaseoccupationaldiseasecriterionMapper dimeBaseoccupationaldiseasecriterionMapper;

    @Autowired
    private DimeBasecontraindicationcriterionMapper dimeBasecontraindicationcriterionMapper;

    @Autowired
    private DimeBaseoccupationaldiseaeMapper dimeBaseoccupationaldiseaeMapper;

    @Autowired
    private DimeBasecontraindicationMapper dimeBasecontraindicationMapper;

    /**
     * 查询职业诊断标准
     *
     * @param id 职业诊断标准主键
     * @return 职业诊断标准
     */
    @Override
    public DimeBaseoccupationcriterion selectDimeBaseoccupationcriterionById(String id) {
        DimeBaseoccupationcriterion _dimeBaseoccupationcriterion = dimeBaseoccupationcriterionMapper.selectDimeBaseoccupationcriterionById(id);
        DimeBaseoccupationcriterion dimeBaseoccupationcriterion = DimeBaseoccupationcriterionList(_dimeBaseoccupationcriterion).stream().findFirst().orElse(null);
        return dimeBaseoccupationcriterion;
    }

    /**
     * 查询职业诊断标准列表
     *
     * @param dimeBaseoccupationcriterion 职业诊断标准
     * @return 职业诊断标准
     */
    @Override
    public List<DimeBaseoccupationcriterion> selectDimeBaseoccupationcriterionList(DimeBaseoccupationcriterion dimeBaseoccupationcriterion) {
        return dimeBaseoccupationcriterionMapper.selectDimeBaseoccupationcriterionList(dimeBaseoccupationcriterion);
    }

    /**
     * 新增职业诊断标准
     *
     * @param dimeBaseoccupationcriterion 职业诊断标准
     * @return 结果
     */
    @Override
    public int insertDimeBaseoccupationcriterion(DimeBaseoccupationcriterion dimeBaseoccupationcriterion) {
        dimeBaseoccupationcriterion.setId(IdUtils.fastUUID());
        dimeBaseoccupationcriterion.setCreateTime(DateUtils.getNowDate());
        dimeBaseoccupationcriterion.setCreateBy(SecurityUtils.getUsername());
        int row = dimeBaseoccupationcriterionMapper.insertDimeBaseoccupationcriterion(dimeBaseoccupationcriterion);

        insertDimeBasecontraindicationcriterion(dimeBaseoccupationcriterion);
        insertDimeBaseoccupationaldiseasecriterion(dimeBaseoccupationcriterion);
        insertDimeBasecombcriterionList(dimeBaseoccupationcriterion);
        return row;
    }

    /**
     * 修改职业诊断标准
     *
     * @param dimeBaseoccupationcriterion 职业诊断标准
     * @return 结果
     */
    @Override
    public int updateDimeBaseoccupationcriterion(DimeBaseoccupationcriterion dimeBaseoccupationcriterion) {
        dimeBaseoccupationcriterion.setUpdateBy(SecurityUtils.getUsername());
        dimeBaseoccupationcriterion.setUpdateTime(DateUtils.getNowDate());

        dimeBasecontraindicationcriterionMapper.deleteDimeBasecontraindicationcriterionByCriterionId(dimeBaseoccupationcriterion.getId());
        dimeBaseoccupationaldiseasecriterionMapper.deleteDimeBaseoccupationaldiseasecriterionByCriterionId(dimeBaseoccupationcriterion.getId());
        dimeBasecombcriterionMapper.deleteDimeBasecombcriterionByCriterionId(dimeBaseoccupationcriterion.getId());

        insertDimeBasecontraindicationcriterion(dimeBaseoccupationcriterion);
        insertDimeBaseoccupationaldiseasecriterion(dimeBaseoccupationcriterion);
        insertDimeBasecombcriterionList(dimeBaseoccupationcriterion);
        return dimeBaseoccupationcriterionMapper.updateDimeBaseoccupationcriterion(dimeBaseoccupationcriterion);
    }

    /**
     * 批量删除职业诊断标准
     *
     * @param ids 需要删除的职业诊断标准主键
     * @return 结果
     */
    @Override
    public int deleteDimeBaseoccupationcriterionByIds(String[] ids) {
        return dimeBaseoccupationcriterionMapper.deleteDimeBaseoccupationcriterionByIds(ids);
    }

    /**
     * 删除职业诊断标准信息
     *
     * @param id 职业诊断标准主键
     * @return 结果
     */
    @Override
    public int deleteDimeBaseoccupationcriterionById(String id) {
        return dimeBaseoccupationcriterionMapper.deleteDimeBaseoccupationcriterionById(id);
    }


    @Override
    public List<DimeBaseoccupationcriterion> DimeBaseoccupationcriterionList(DimeBaseoccupationcriterion dimeBaseoccupationcriterion) {

        List<DimeBaseoccupationcriterion> dimeBaseoccupationcriterionList = dimeBaseoccupationcriterionMapper.selectDimeBaseoccupationcriterionList(dimeBaseoccupationcriterion);

        for (DimeBaseoccupationcriterion dimeBaseoccupationcriterion1 : dimeBaseoccupationcriterionList)
        {
            //危害必检项目
            DimeBasecombcriterion searchBasecombcriterion = new DimeBasecombcriterion();
            searchBasecombcriterion.setCriterionId(dimeBaseoccupationcriterion1.getId());
            List<DimeBasecombcriterion> dimeBasecombcriterionList = dimeBasecombcriterionMapper.selectDimeBasecombcriterionList(searchBasecombcriterion);
            dimeBaseoccupationcriterion1.setDimeBasecombcriterionList(dimeBasecombcriterionList);

            //危害禁忌症
            DimeBasecontraindicationcriterion searchBasecontraindicationcriterion = new DimeBasecontraindicationcriterion();
            searchBasecontraindicationcriterion.setCriterionId(dimeBaseoccupationcriterion1.getId());

            List<DimeBasecontraindicationcriterion> dimeBasecontraindicationcriterionList = dimeBasecontraindicationcriterionMapper.selectDimeBasecontraindicationcriterionList(searchBasecontraindicationcriterion);
            dimeBaseoccupationcriterion1.setDimeBasecontraindicationcriterionList(dimeBasecontraindicationcriterionList);

            //危害 职业病
            DimeBaseoccupationaldiseasecriterion searchBaseoccupationaldiseasecriterion = new DimeBaseoccupationaldiseasecriterion();
            searchBaseoccupationaldiseasecriterion.setCriterionId(dimeBaseoccupationcriterion1.getId());
            List<DimeBaseoccupationaldiseasecriterion> dimeBaseoccupationaldiseasecriterionList = dimeBaseoccupationaldiseasecriterionMapper.selectDimeBaseoccupationaldiseasecriterionList(searchBaseoccupationaldiseasecriterion);
            dimeBaseoccupationcriterion1.setDimeBaseoccupationcriterionList(dimeBaseoccupationaldiseasecriterionList);


        }

        return dimeBaseoccupationcriterionList;


    }

    @Override
    public List<DimeBaseoccupationcriterion> getBaseoccupationcriterionListByHarms(List<String> harmIds) {
        return null;
    }

    public void insertDimeBaseoccupationaldiseasecriterion(DimeBaseoccupationcriterion dimeBaseoccupationcriterion) {
        List<DimeBaseoccupationaldiseasecriterion> dimeBaseoccupationcriterionList = dimeBaseoccupationcriterion.getDimeBaseoccupationcriterionList();
        if (dimeBaseoccupationcriterionList != null && !dimeBaseoccupationcriterionList.isEmpty()) {
            for (DimeBaseoccupationaldiseasecriterion dimeBaseoccupationaldiseasecriterion : dimeBaseoccupationcriterionList) {
                DimeBaseoccupationaldiseae dimeBaseoccupationaldiseae = dimeBaseoccupationaldiseaeMapper.selectDimeBaseoccupationaldiseaeById(dimeBaseoccupationaldiseasecriterion.getOccupationaldiseaeId());
                if (dimeBaseoccupationaldiseae!=null){
                    dimeBaseoccupationaldiseasecriterion.setCriterionId(dimeBaseoccupationcriterion.getId());
                    dimeBaseoccupationaldiseasecriterion.setOccupationaldiseaeName(dimeBaseoccupationaldiseae.getName());
                    if (StringUtils.isEmpty(dimeBaseoccupationaldiseasecriterion.getId())) {
                        dimeBaseoccupationaldiseasecriterion.setId(IdUtils.fastUUID());
                        dimeBaseoccupationaldiseasecriterion.setCreateBy(SecurityUtils.getUsername());
                        dimeBaseoccupationaldiseasecriterion.setCreateTime(DateUtils.getNowDate());
                    }else{
                        dimeBaseoccupationaldiseasecriterion.setUpdateBy(SecurityUtils.getUsername());
                        dimeBaseoccupationaldiseasecriterion.setUpdateTime(DateUtils.getNowDate());
                    }
                    dimeBaseoccupationaldiseasecriterionMapper.insertDimeBaseoccupationaldiseasecriterion(dimeBaseoccupationaldiseasecriterion);
                }
            }
        }
    }

    public void insertDimeBasecontraindicationcriterion(DimeBaseoccupationcriterion dimeBaseoccupationcriterion) {
        List<DimeBasecontraindicationcriterion> dimeBasecontraindicationcriterionList = dimeBaseoccupationcriterion.getDimeBasecontraindicationcriterionList();
        if (dimeBasecontraindicationcriterionList != null && !dimeBasecontraindicationcriterionList.isEmpty()) {
            for (DimeBasecontraindicationcriterion dimeBasecontraindicationcriterion : dimeBasecontraindicationcriterionList) {
                DimeBasecontraindication dimeBasecontraindication = dimeBasecontraindicationMapper.selectDimeBasecontraindicationById(dimeBasecontraindicationcriterion.getCationcriterionId());
                if (dimeBasecontraindication!=null){
                    dimeBasecontraindicationcriterion.setCriterionId(dimeBaseoccupationcriterion.getId());
                    dimeBasecontraindicationcriterion.setCationcriterionName(dimeBasecontraindication.getName());
                    if (StringUtils.isEmpty(dimeBasecontraindicationcriterion.getId())) {
                        dimeBasecontraindicationcriterion.setId(IdUtils.fastUUID());
                        dimeBasecontraindicationcriterion.setCreateBy(SecurityUtils.getUsername());
                        dimeBasecontraindicationcriterion.setCreateTime(DateUtils.getNowDate());
                    }else {
                        dimeBasecontraindicationcriterion.setUpdateBy(SecurityUtils.getUsername());
                        dimeBasecontraindicationcriterion.setUpdateTime(DateUtils.getNowDate());
                    }
                    dimeBasecontraindicationcriterionMapper.insertDimeBasecontraindicationcriterion(dimeBasecontraindicationcriterion);
                }
            }
        }
    }

    public void insertDimeBasecombcriterionList(DimeBaseoccupationcriterion dimeBaseoccupationcriterion){
        List<DimeBasecombcriterion> dimeBasecombcriterionList = dimeBaseoccupationcriterion.getDimeBasecombcriterionList();
        if (dimeBasecombcriterionList!=null &&!dimeBasecombcriterionList.isEmpty()) {
            for (DimeBasecombcriterion dimeBasecombcriterion : dimeBasecombcriterionList) {
                DimeBasecomb dimeBasecomb = dimeBasecombMapper.selectDimeBasecombById(dimeBasecombcriterion.getCombId());
                if (dimeBasecomb != null) {
                    dimeBasecombcriterion.setCriterionId(dimeBaseoccupationcriterion.getId());
                    dimeBasecombcriterion.setCombName(dimeBasecomb.getCombName());
                    if (!Objects.equals(dimeBasecombcriterion.getRequired(), "1")){
                        dimeBasecombcriterion.setRequired("0");
                    }
                    if (StringUtils.isEmpty(dimeBasecombcriterion.getId())) {
                        dimeBasecombcriterion.setId(IdUtils.fastUUID());
                        dimeBasecombcriterion.setCreateBy(SecurityUtils.getUsername());
                        dimeBasecombcriterion.setCreateTime(DateUtils.getNowDate());
                    }else{
                        dimeBasecombcriterion.setUpdateBy(SecurityUtils.getUsername());
                        dimeBasecombcriterion.setUpdateTime(DateUtils.getNowDate());
                    }
                    dimeBasecombcriterionMapper.insertDimeBasecombcriterion(dimeBasecombcriterion);
                }
            }
        }
    }
}

